// 店铺模型
const { Store, validateStore } = require('../../../models/store');
// 商品模型
const { Goods } = require('../../../models/good');
// 统一数据返回类
const { DataReturn } = require('../../../common/returnclass');

/**
 * @api {Delete} /stores/:id 删除店铺
 * @apiGroup Admin-Stores
 *
 * @apiParam {ObjectId} id 店铺id
 *
 * @apiSuccess {Number} code 状态码
 * @apiSuccess {Object} data 返回数据
 * @apiSuccess {String} msg  返回消息
 */
module.exports = async (req, res) => {
	// 店铺id
	let {id } = req.params;
	console.log(req.params)

	// 查询店铺
	let stores = await Store.findOne({ _id:id });
	// 商品已存在
	if (!stores) return DataReturn.getData(res,400,[],'该店铺不存在');
	// 保存商品信息
	Store.findOneAndDelete({ _id: id })
	.then(()=>{
		return stores.store_goods.forEach(async item=>{
			await Goods.findOneAndDelete({ _id: item })
		})
	}).then(()=>{
		DataReturn.getData(res,200,[],'删除店铺成功')
	}).catch(()=>{
		DataReturn.getData(res,400,[],'删除店铺失败')
	})

}